//
//  CodeScanAffirmViewController.m
//  HTHappySale
//
//  Created by 创维海通 on 2017/9/22.
//  Copyright © 2017年 zxyywy. All rights reserved.
//

#import "CodeScanAffirmViewController.h"
#import "MeUserInfoViewController.h"

//Model
#import "UserInfoModel.h"

//Request
#import "HTTPPromise+Me.h"

//Cell
#import "ScanAffirmCell.h"

//Tools
#import "AppUtil.h"
#import "CellClearCache.h"

@interface CodeScanAffirmViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UserInfoModel *userModel;
@property (assign, nonatomic) int affirmId;
@property (strong, nonatomic) IBOutlet UITableView *AffirmTableView;
@end

@implementation CodeScanAffirmViewController

#pragma mark -init
#define AffirmIdentifyCell @"AffirmCell"

- (instancetype)init:(UserInfoModel *)model AtWithMesaageId:(int)messageId{
    if(self = [super init])
    {
        _userModel = [[UserInfoModel alloc] init];
        _userModel = model;
        _affirmId = messageId;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self addNavigationLeftView:@"" andType:0];
    [self addNavigationMiddleView:@"信息确认"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //Xib
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([ScanAffirmCell class])
                                bundle:[NSBundle mainBundle]];
    [_AffirmTableView registerNib:nib forCellReuseIdentifier:AffirmIdentifyCell];
    
    [self initTableView];
}

- (void)initTableView{
    _AffirmTableView.tableFooterView = [[UIView alloc] init];
    _AffirmTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _AffirmTableView.scrollEnabled = NO;
}

- (void)submitRequest{
    NSDictionary *affirmParam = @{@"userId":[NSNumber numberWithInt:_affirmId]};
    NSData *jsonData;
    
    if ([NSJSONSerialization isValidJSONObject:affirmParam])
    {
        NSError *error;
        jsonData = [NSJSONSerialization dataWithJSONObject:affirmParam options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"json data:%@",json);
    }
    __weak __typeof(self)weakSelf = self;
    [HTTPPromise requestBindManagerParams:jsonData Success:^(id responseObj) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        NSLog(@"%@",responseObj);
        int successRet =[responseObj[@"ret"] intValue];
        //(数据转模型）
        if (successRet == 0) {
            [strongSelf returnCollecter];
        }else{
            [AppUtil showTipMessage:responseObj[@"desc"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error.userInfo);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

#define cellIdentify @"cell"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ScanAffirmCell *affirmCell = [tableView dequeueReusableCellWithIdentifier:AffirmIdentifyCell];
        affirmCell = [CellClearCache clearCache:affirmCell];
        
        [affirmCell configureCell:_userModel];
        affirmCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return affirmCell;
    }else{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
        }
        
        cell = [CellClearCache clearCache:cell];
        
        if(indexPath.row == 1)
        {
            UIButton *affirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [affirmBtn setTitle:@"确认" forState:UIControlStateNormal];
            affirmBtn.frame = CGRectMake((SCREEN_WIDTH - 120 * HTCFAppWidthScale) / 2, 10, 120 * HTCFAppWidthScale, 30 * HTCFAppHeightScale);
            [affirmBtn setBackgroundColor:HTCFQuickRGBColor(51,190,0)];
            affirmBtn.layer.borderColor = [UIColor blackColor].CGColor;
            affirmBtn.layer.borderWidth = 1.f;
            affirmBtn.layer.cornerRadius = 4.0;
            [affirmBtn setTitleColor: [UIColor whiteColor] forState:UIControlStateNormal];
            affirmBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            [affirmBtn addTarget:self action:@selector(submitRequest) forControlEvents:UIControlEventTouchUpInside];
            
            [cell addSubview:affirmBtn];
        }else if (indexPath.row == 2)
        {
            UIButton *callOffBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [callOffBtn setTitle:@"返回" forState:UIControlStateNormal];
            callOffBtn.frame = CGRectMake((SCREEN_WIDTH - 120 * HTCFAppWidthScale) / 2, 10, 120 * HTCFAppWidthScale, 30 * HTCFAppHeightScale);
            [callOffBtn setBackgroundColor:HTCFQuickRGBColor(247,247,247)];
            callOffBtn.layer.borderColor = [UIColor blackColor].CGColor;
            callOffBtn.layer.borderWidth = 1.f;
            callOffBtn.layer.cornerRadius = 4.0;
            
            [callOffBtn setTitleColor: [UIColor blackColor] forState:UIControlStateNormal];
            callOffBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
            
            [callOffBtn addTarget:self action:@selector(returnCollecter) forControlEvents:UIControlEventTouchUpInside];
            
            [cell addSubview:callOffBtn];
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 90;
    }
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
    {
        return 90;
    }
    return 50;
}

#pragma mark -Action
- (void)returnCollecter
{
    NSArray *controllerArr = [self.navigationController viewControllers];
    for (UIViewController *controller in controllerArr) {
        if ([controller isKindOfClass:[MeUserInfoViewController class]]) {
            [self.navigationController popToViewController:controller animated:YES];
        }
    }
}

//- (IBAction)backAction:(UITapGestureRecognizer *)sender {
//    [self returnCollecter];
//}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
